CREATE TABLE tb_account(
	id NUMBER PRIMARY KEY,
	auth NUMBER NOT NULL,
	name VARCHAR2(128) NOT NULL,
	password VARCHAR2(128) NOT NULL,
	money NUMBER DEFAULT 0.0,
	CONSTRAINT uk_name UNIQUE(name)
);
-- 创建 账户表

CREATE SEQUENCE inc_account 
	INCREMENT BY 1
	START WITH 1
	NOMAXVALUE
	NOMINVALUE
	NOCACHE;
-- 创建 自增序列
	
CREATE OR REPLACE TRIGGER tb_account_inc_trg BEFORE
	INSERT ON tb_account FOR EACH ROW WHEN(NEW.id IS NULL)
BEGIN
	SELECT inc_account.NEXTVAL INTO :NEW.id FROM DUAL;
END;
-- 为 账户表 创建 自增触发器

CREATE TABLE tb_product(
	id NUMBER PRIMARY KEY,
	vendor_id NUMBER,
	name VARCHAR2(128) NOT NULL,
	price NUMBER NOT NULL,
	amount NUMBER DEFAULT 0,
	status NUMBER DEFAULT 0,
	CONSTRAINT fk_vender_id FOREIGN KEY(vendor_id) REFERENCES TB_ACCOUNT(id)
);
-- 创建 商品表

CREATE SEQUENCE inc_product
	INCREMENT BY 1
	START WITH 1
	NOMAXVALUE
	NOMINVALUE
	NOCACHE;
-- 创建 自增序列

CREATE OR REPLACE TRIGGER tb_product_inc_trg BEFORE
	INSERT ON tb_product FOR EACH ROW WHEN(NEW.id IS NULL)
BEGIN
	SELECT inc_product.NEXTVAL INTO :NEW.id FROM DUAL;
END;
-- 为 商品表 创建 自增触发器

CREATE TABLE tb_order(
	id NUMBER PRIMARY KEY,
	customer_id NUMBER,
	product_id NUMBER,
	status NUMBER NOT NULL,
	amount NUMBER NOT NULL,
	price NUMBER NOT NULL,
	cdate DATE NOT NULL,
	CONSTRAINT fk_customer_id FOREIGN KEY(customer_id) REFERENCES TB_ACCOUNT(id),
	CONSTRAINT fk_product_id FOREIGN KEY(product_id) REFERENCES TB_PRODUCT(id)
);
-- 创建 订单表

CREATE SEQUENCE inc_order
	INCREMENT BY 1
	START WITH 1
	NOMAXVALUE
	NOMINVALUE
	NOCACHE;
-- 创建 自增序列

CREATE OR REPLACE TRIGGER tb_order_inc_trg BEFORE
	INSERT ON tb_order FOR EACH ROW WHEN(NEW.id IS NULL)
BEGIN
	SELECT inc_order.NEXTVAL INTO :NEW.id FROM DUAL;
END;
-- 为 订单表创建 自增触发器

